//
//  KDCalendarSettingView.m
//  LaiKeDaoClient
//
//  Created by dongkedi的iMac on 15/11/6.
//  Copyright © 2015年 LKD. All rights reserved.
//

#import "KDCalendarSettingView.h"
#import "KDSettingCollectionViewCell.h"

NSString *const KDCalendarCellIdentifier = @"cell";
#define collectionBackColor @"#cccccc"
//
@implementation KDCalendarSettingView

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self createUI];
    }
    return self;
}

- (void)createUI
{
    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    self.collectionView = [[UICollectionView alloc] initWithFrame:(CGRect){45/2,20,WIDTH(self)-45,83/2*7+58/2} collectionViewLayout:flowLayout];
    [_collectionView setBackgroundColor:[UIColor colorWithHexString:collectionBackColor]];
    [_collectionView registerClass:[KDSettingCollectionViewCell class] forCellWithReuseIdentifier:KDCalendarCellIdentifier];
    _weekDayArray = @[@"周一",@"周二",@"周三",@"周四",@"周五",@"周六",@"周日"];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [self addSubview:_collectionView];
    
    self.startAndEndView = [[KDStartAndEndView alloc] initWithFrame:(CGRect){0,Y(_collectionView)+HEIGHT(_collectionView),kScreenWidth,35/2+12+15*2+34/2+70/2+20}];
    self.startAndEndView.startAndEndDelegate = self;
    [self addSubview:_startAndEndView];
}

- (void)startAndEndViewSureButton:(NSString *)startDate endDate:(NSString *)endDate
{
    if ([self.calendarSettingDelegate respondsToSelector:@selector(calendarSettingSureButton:startDate:endDate:)]) {
        NSString *str = [self getReturn_set_outcall];
        [self.calendarSettingDelegate calendarSettingSureButton:str startDate:startDate endDate:endDate];
    }
}

- (NSString *)getReturn_set_outcall
{
    NSMutableDictionary *dic1 = [NSMutableDictionary dictionary];
    for (int i = 1; i < 8; i++) {
        for (int j = 1; j < 4; j++) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:j inSection:i];
            KDSettingCollectionViewCell *cell = (KDSettingCollectionViewCell *)[_collectionView cellForItemAtIndexPath:indexPath];
            
            [dic1 setObject:cell.cellIsSelect ? @"1" : @"0" forKey:[NSString stringWithFormat:@"%d",j+(i-1)*3]];
            
        }
    }
    
    return [dic1 jsonEncodedKeyValueString];
}

#pragma -mark collectionView delegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 8;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 4;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            return CGSizeMake(126/2 -0.5, 58 / 2 -0.15);
        }else{
            return CGSizeMake(142/2 -0.5, 58 / 2 -0.15);
        }
    }else{
        if (indexPath.row == 0) {
            return CGSizeMake(126/2 -0.5, 83 / 2 -0.15);
        }else{
            return CGSizeMake(142/2 -0.5, 83 / 2 -0.15);
        }
    }
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    KDSettingCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:KDCalendarCellIdentifier forIndexPath:indexPath];
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            //
        }else if (indexPath.row == 1){
            cell.dateLabel.text = @"上午";
        }else if (indexPath.row == 2){
            cell.dateLabel.text = @"下午";
        }else if (indexPath.row == 3){
            cell.dateLabel.text = @"晚上";
        }
    } else {
        if (indexPath.row == 0) {
            //
            cell.dateLabel.text = [_weekDayArray objectAtIndex:indexPath.section-1];
        }else {
            cell.starImageView.hidden = NO;
        }
    }
    return cell;
}

- (void)reloadCollectionView:(NSDictionary *)dataDic
{
//    self.dataDic = dataDic;
    for (int i = 1; i < 8; i++) {
        for (int j = 1; j < 4; j++) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:j inSection:i];
            KDSettingCollectionViewCell *cell = (KDSettingCollectionViewCell *)[_collectionView cellForItemAtIndexPath:indexPath];
            if ([[dataDic objectForKey:[NSString stringWithFormat:@"%d",j+(i-1)*3]] isEqualToString:@"1"]) {
                //
                [cell.starImageView setImage:[UIImage imageNamed:@"star_visit.png"]];
                cell.cellIsSelect = YES;
            }
        }
    }
    
}

- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return NO;
    }
    if (indexPath.row == 0) {
        return NO;
    }
    return YES;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    KDSettingCollectionViewCell *cell = (KDSettingCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    if (cell.cellIsSelect) {
        [cell.starImageView setImage:[UIImage imageNamed:@"star_visit_empty.png"]];
        cell.cellIsSelect = NO;
    }else{
        [cell.starImageView setImage:[UIImage imageNamed:@"star_visit.png"]];
        cell.cellIsSelect = YES;
    }
}
@end
